<%@ page session="true" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<!--
Tasks:
sexy up the background
sexy up the submit button
-->
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Choose your location</title>
  <script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAkX4jjRDYEMLIfiC7Fs6ZFBRlOb26qSyU154aZeLwOrF4C7-DphRUK1aqWRbYpF4qQPq5mH8D7De9WQ">
</script>

 <link type="text/css"	href="css/submit.css" rel="stylesheet" />
<style type="text/css" media="screen">
body {
margin:0;
padding:0;
height:100%;
}

#mapsLocation {
min-height:400px; /*ff*/
height: auto !important; /*ie7 */
height: 400px; /*ie6 */
padding:0;
width:95%;
color:#333;
background:#eaeaea;
border:1px solid #333;
} 

p.info
{
	-moz-border-radius:20px 20px 20px 20px;
-moz-box-shadow:0 2px 6px rgba(0, 0, 0, 0.5), 0 1px rgba(255, 255, 255, 0.3) inset, 0 10px rgba(255, 255, 255, 0.2) inset, 0 10px 20px rgba(255, 255, 255, 0.25) inset, 0 -15px 30px rgba(0, 0, 0, 0.3) inset;
background:none repeat scroll 0 0 rgba(0, 0, 0, 0.25);
border:1px solid rgba(0, 0, 0, 0.5);
font-size:1.3em;
padding:0.5em 0.8em;
font-family:Consolas,'Andale Mono','Courier New',monospace;
text-shadow:0.05em 0.05em 0.1em black;
color:white;
}
</style>

<script type="text/javascript">

 // var _gaq = _gaq || [];
 // _gaq.push(['_setAccount', 'UA-17940542-1']);
 // _gaq.push(['_trackPageview']);

  //(function() {
   // var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  //  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  //  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  //})();

</script>

<script type="text/javascript" src="js/jquery-nightly.js">       </script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js">       </script>

<script src="js/jquery.alerts.js" type="text/javascript"></script>

<link href="css/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />

</head>


<body>

<div id="mapsLocation">
<img src="images/loading.gif">
</div>
<form:form method="post" action="location" name="locationform" onsubmit="return checkvalidity()">



<div id="controlPanel" >
<p class="info">
Click on the location where you want to build your home city and then click continue.
</p>
<p align="right">
<input type="submit" value="Continue...">
<input type="hidden" name="cCode" value="${cd}">
<input type="hidden" name="lat" value="37.4419">
<input type="hidden" name="lon" value="-122.1419">


</p>
</div>
</form:form>

<script language="javascript">
var isValid=true;
var homeLat=37.4419;
var homeLon=-122.1419;
//BUGFIX: if undefined send him back to country page
var country="${cd}";
var map;
var homeMarker;
var geocoder;
var latlng=new GLatLng(homeLat,homeLon );

function checkvalidity()
{
if(!isValid)
{
jAlert("Place your home on a valid location in your country " + country+ " before continuing","Invalid Location");
return false;
}
else
return true;
}

function initMap()
{
geocoder = new GClientGeocoder();
geocoder.setBaseCountryCode(country);
geocoder.getLatLng(country,showMap);
}

 function showMap(latlng) {
  map = new GMap2(document.getElementById("mapsLocation"));
  map.setCenter(latlng, 5);
  map.addControl(new GSmallMapControl());
  
 document.locationform["lon"].value=latlng.lng();
 document.locationform["lat"].value=latlng.lat();;
    
	  GEvent.addListener(map, "click", function(overlay,latlng) {
if(latlng==null) return;
		    map.removeOverlay(homeMarker); 
                     homeMarker = new GMarker(latlng, {draggable: false});  
 					map.addOverlay(homeMarker); 
		 			 geocoder.getLocations(latlng, showAddress);
		});
	  homeMarker = new GMarker(latlng, {draggable: false}); 
	  map.addOverlay(homeMarker); 
    }

function showAddress(response) {

  if (!response || response.Status.code != 200) {
   jAlert("This is a disputed location.<br>You cannot setup your base in this area!", "Disputed Area");
   isValid=false;
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    document.locationform["lon"].value=place.Point.coordinates[0];
    document.locationform["lat"].value=place.Point.coordinates[1];
    
 // homeMarker.openInfoWindowHtml(   'Setting new home at ' + place.address + '</br>' + ' in country ' + place.AddressDetails.Country.CountryNameCode);
 homeMarker.openInfoWindowHtml('Setting home location at :<b>' + place.address +"</b>");


if(place.AddressDetails.Country.CountryNameCode!=country)
{
isValid=false;
jAlert("You can setup your base only in your home country<br> Please click anywhere inside your country to setup your base","This is not your country");
}
else
{
isValid=true;
}

  }
}

initMap();
</script>

</body>
</html>
